package com.bluesky.controller;

import com.bluesky.comm.Result;
import com.bluesky.comm.ResultUtils;
import com.bluesky.ds.ReadWriteRoutingStrategyHolder;
import com.bluesky.po.UserPO;
import com.bluesky.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("lesson027")
@Slf4j
public class Lesson027Controller {

    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public Result<Map<String, UserPO>> test(){
        //没有路由选择
        UserPO userPO = userService.getUser(1);
        //走主库获取用户信息
        UserPO userFromMaster = userService.getUserFromMaster();
        //走从库获取用户信息
        UserPO userFromSlave = userService.getUserFromSlave();
        //强制走主库
        UserPO userHitMaster = ReadWriteRoutingStrategyHolder.hitMaster(()->this.userService.getUserFromSlave());

        //封装结果
        Map<String, UserPO> result = new LinkedHashMap<>();
        result.put("user", userPO);
        result.put("userFromMaster", userFromMaster);
        result.put("userFromSlave", userFromSlave);
        result.put("userHitMaster", userHitMaster);

        return ResultUtils.success(result);
    }

}
